<?php 
class ControllerLocalisationCurrency extends AdminController {
 
	public function index() {
		
		M('localisation/currency');
		
		$this->getList();
	}

	public function insert() {
		
		M('localisation/currency');
		
		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_currency->addCurrency($this->request->post);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/currency'));
		}
		
		$this->getForm();
	}

	public function update() {
		
		M('localisation/currency');
		
		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_currency->editCurrency($this->request->get['currency_id'], $this->request->post);
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/currency'));
		}

		$this->getForm();
	}

	public function delete() {
		
		M('localisation/currency');
		
		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $currency_id) {
				$this->model_localisation_currency->deleteCurrency($currency_id);
			}
			
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/currency'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
		
		$qf = new Query_filter();
		
		$currency_total = $this->model_localisation_currency->getTotalCurrencies();

		$this->data['currencies'] = $this->model_localisation_currency->getCurrencies($qf);
		
		$this->data['pagination'] = $this->pagination($currency_total)->render();
		
		$this->display('localisation/currency_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);
		
		if (isset($this->request->get['currency_id'])) {
			$this->data['action'] = UA('localisation/currency/update', 'currency_id=' . $this->request->get['currency_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_localisation_currency->getCurrency($this->request->get['currency_id']);
			}
		} else {
			$this->data['action'] = UA('localisation/currency/insert');
		}
		
		$this->display('localisation/currency_form.tpl');
	}
	
	protected function validateForm() { 
		$pass = true;
		if (!range_length($this->request->post['title'], 3, 32)) {
			$this->setMessage('error_title', $this->_['error_title']);
			$pass = false;
		}

		if (utf8_strlen($this->request->post['code']) != 3) {
			$this->setMessage('error_code', $this->_['error_code']);
			$pass = false;
		}
		
		return $pass;
	}

	protected function validateDelete() {
		M('setting/store');
		M('sale/order');
		
		foreach ($this->request->post['selected'] as $currency_id) {
			$currency_info = $this->model_localisation_currency->getCurrency($currency_id);

			if ($currency_info) {
				if (C('config_currency') == $currency_info['code']) {
					$this->setMessage('error_warning', $this->_['error_default']);
					return false;
				}
				
				$store_total = $this->model_setting_store->getTotalStoresByCurrency($currency_info['code']);
	
				if ($store_total) {
					$this->setMessage('error_warning', sprintf($this->_['error_store'], $store_total));
					return false;
				}					
			}
			
			$order_total = $this->model_sale_order->getTotalOrdersByCurrencyId($currency_id);

			if ($order_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_order'], $order_total));
				return false;
			}					
		}
		return true;
	}	
}
?>